/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.product;

import java.util.List;

import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategoryColor;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategoryProperty;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategorySpec;
import com.thinkgem.jeesite.modules.hk.shop.entity.product.*;
import com.thinkgem.jeesite.modules.hk.shop.entity.property.HkShopPropertyValue;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategoryColorService;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategorySpecService;
import com.thinkgem.jeesite.modules.hk.shop.service.property.HkShopPropertyService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.shop.dao.product.HkShopProductDao;

/**
 * 乐购商品Service
 * @author 罗天文
 * @version 2018-05-16
 */
@Service
@Transactional(readOnly = true)
public class HkShopProductService extends CrudService<HkShopProductDao, HkShopProduct> {
	@Autowired
	private HkShopProductPropertyService hkShopProductPropertyService;
	@Autowired
	private HkShopPropertyService hkShopPropertyService;
    @Autowired
    private HkShopProductImagesService hkShopProductImagesService;

    @Autowired
    private HkShopProductSkuService hkShopProductSkuService;


    @Autowired
    private HkShopProductSkuImagesService hkShopProductSkuImagesService;

    @Autowired
    private HkShopCategoryColorService hkShopCategoryColorService;


    @Autowired
    private HkShopCategorySpecService hkShopCategorySpecService;
    @Autowired
    private HkShopProductColorService hkShopProductColorService;


    @Autowired
    private HkShopProductSpecService hkShopProductSpecService;


    public HkShopProduct get(String id) {

		HkShopProduct p=super.get(id);
		HkShopProductProperty hkShopProductProperty=new HkShopProductProperty();
		hkShopProductProperty.setProduct(p);

		List<HkShopProductProperty> list= hkShopProductPropertyService.findList(hkShopProductProperty);
		for (HkShopProductProperty pp:list
				) {
			pp.setProperty(hkShopPropertyService.get(pp.getProperty().getId()));
		}

		p.setHkShopProductPropertyList(list);



        HkShopProductImages hkShopProductImages=new HkShopProductImages();
        hkShopProductImages.setProduct(p);

        List<HkShopProductImages> img= hkShopProductImagesService.findList(hkShopProductImages);

        p.setHkShopProductImagesList(img);


        HkShopCategoryColor hkShopCategoryColor=new HkShopCategoryColor();
        hkShopCategoryColor.setCategory(p.getCategory());

        List<HkShopCategoryColor> categorycolors= hkShopCategoryColorService.findList(hkShopCategoryColor);

        p.setCategorycolorsList(categorycolors);


        HkShopCategorySpec hkShopCategorySpec=new HkShopCategorySpec();
        hkShopCategorySpec.setCategory(p.getCategory());

        List<HkShopCategorySpec> categorySpecs= hkShopCategorySpecService.findList(hkShopCategorySpec);

        p.setCategoryspecsList(categorySpecs);




        HkShopProductColor hkShopProductColor=new HkShopProductColor();
        hkShopProductColor.setProduct(p);

        List<HkShopProductColor> colorsList= hkShopProductColorService.findList(hkShopProductColor);

        p.setColorsList(colorsList);


        HkShopProductSpec hkShopProductSpec=new HkShopProductSpec();
        hkShopProductSpec.setProduct(p);

        List<HkShopProductSpec> hkShopProductSpecs= hkShopProductSpecService.findList(hkShopProductSpec);

        p.setSpecsList(hkShopProductSpecs);







        HkShopProductSku hkShopProductSku=new HkShopProductSku();
        hkShopProductSku.setProduct(p);
        List<HkShopProductSku> skus= hkShopProductSkuService.findList(hkShopProductSku);
        HkShopProductSkuImages hkShopProductSkuImages=new HkShopProductSkuImages();
        for (HkShopProductSku sku:skus
                ) {
             hkShopProductSkuImages.setSku(sku);
             sku.setSkuImagesList(hkShopProductSkuImagesService.findList(hkShopProductSkuImages));
        }
        p.setSkusList(skus);



		return p;
	}
	
	public List<HkShopProduct> findList(HkShopProduct hkShopProduct) {
		return super.findList(hkShopProduct);
	}
	
	public Page<HkShopProduct> findPage(Page<HkShopProduct> page, HkShopProduct hkShopProduct) {
		return super.findPage(page, hkShopProduct);
	}
	
	@Transactional(readOnly = false)
	public void save(HkShopProduct hkShopProduct) {
		if (hkShopProduct.getDescript()!=null){
			hkShopProduct.setDescript(StringEscapeUtils.unescapeHtml4(
					hkShopProduct.getDescript()));
		}
        hkShopProduct.setImgSrc(hkShopProduct.getHkShopProductImagesList().get(0).getImgSrc());
		super.save(hkShopProduct);
		for (HkShopProductProperty hkShopProductProperty:hkShopProduct.getHkShopProductPropertyList()
			 ) {
			 hkShopProductProperty.setProduct(hkShopProduct);
			 if(null!=hkShopProductProperty.getPropertyvalue()&&hkShopProductProperty.getPropertyvalue().length>0) {
				 hkShopProductProperty.setPropertyvalues(StringUtils.join(hkShopProductProperty.getPropertyvalue(), ','));
			 }

			hkShopProductPropertyService.save(hkShopProductProperty);
		}
        for (HkShopProductImages hkShopProductImages:hkShopProduct.getHkShopProductImagesList()
                ) {
            hkShopProductImages.setIsmain("1");
            hkShopProductImages.setProduct(hkShopProduct);
            if (HkShopProductImages.DEL_FLAG_NORMAL.equals(hkShopProductImages.getDelFlag())){
                    hkShopProductImagesService.save(hkShopProductImages);
            }else{
                hkShopProductImagesService.delete(hkShopProductImages);
            }

        }


	}
	
	@Transactional(readOnly = false)
	public void delete(HkShopProduct hkShopProduct) {
		super.delete(hkShopProduct);
	}
	
}